{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Features Extraction with PCA"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas as pd\n",
        "\n",
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "\n",
        "# fix_yahoo_finance is used to fetch data \n",
        "import yfinance as yf\n",
        "yf.pdr_override()"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:05.921Z",
          "iopub.execute_input": "2020-07-01T01:06:05.927Z",
          "iopub.status.idle": "2020-07-01T01:06:07.121Z",
          "shell.execute_reply": "2020-07-01T01:06:07.153Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# input\n",
        "symbol = 'AMD'\n",
        "start = '2014-01-01'\n",
        "end = '2019-01-01'\n",
        "\n",
        "# Read data \n",
        "dataset = yf.download(symbol,start,end)\n",
        "\n",
        "# View Columns\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[*********************100%***********************]  1 of 1 completed\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 2,
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Adj Close</th>\n",
              "      <th>Close</th>\n",
              "      <th>High</th>\n",
              "      <th>Low</th>\n",
              "      <th>Open</th>\n",
              "      <th>Volume</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Date</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>2014-01-02</th>\n",
              "      <td>3.95</td>\n",
              "      <td>3.95</td>\n",
              "      <td>3.98</td>\n",
              "      <td>3.84</td>\n",
              "      <td>3.85</td>\n",
              "      <td>20548400</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2014-01-03</th>\n",
              "      <td>4.00</td>\n",
              "      <td>4.00</td>\n",
              "      <td>4.00</td>\n",
              "      <td>3.88</td>\n",
              "      <td>3.98</td>\n",
              "      <td>22887200</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2014-01-06</th>\n",
              "      <td>4.13</td>\n",
              "      <td>4.13</td>\n",
              "      <td>4.18</td>\n",
              "      <td>3.99</td>\n",
              "      <td>4.01</td>\n",
              "      <td>42398300</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2014-01-07</th>\n",
              "      <td>4.18</td>\n",
              "      <td>4.18</td>\n",
              "      <td>4.25</td>\n",
              "      <td>4.11</td>\n",
              "      <td>4.19</td>\n",
              "      <td>42932100</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2014-01-08</th>\n",
              "      <td>4.18</td>\n",
              "      <td>4.18</td>\n",
              "      <td>4.26</td>\n",
              "      <td>4.14</td>\n",
              "      <td>4.23</td>\n",
              "      <td>30678700</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "            Adj Close  Close  High   Low  Open    Volume\n",
              "Date                                                    \n",
              "2014-01-02       3.95   3.95  3.98  3.84  3.85  20548400\n",
              "2014-01-03       4.00   4.00  4.00  3.88  3.98  22887200\n",
              "2014-01-06       4.13   4.13  4.18  3.99  4.01  42398300\n",
              "2014-01-07       4.18   4.18  4.25  4.11  4.19  42932100\n",
              "2014-01-08       4.18   4.18  4.26  4.14  4.23  30678700"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 2,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:07.134Z",
          "iopub.execute_input": "2020-07-01T01:06:07.141Z",
          "iopub.status.idle": "2020-07-01T01:06:08.490Z",
          "shell.execute_reply": "2020-07-01T01:06:08.509Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset['Open_Close'] = (dataset['Open'] - dataset['Adj Close'])/dataset['Open']\n",
        "dataset['High_Low'] = (dataset['High'] - dataset['Low'])/dataset['Low']\n",
        "dataset['Increase_Decrease'] = np.where(dataset['Volume'].shift(-1) > dataset['Volume'],1,0)\n",
        "dataset['Buy_Sell_on_Open'] = np.where(dataset['Open'].shift(-1) > dataset['Open'],1,0)\n",
        "dataset['Buy_Sell'] = np.where(dataset['Adj Close'].shift(-1) > dataset['Adj Close'],1,0)\n",
        "dataset['Returns'] = dataset['Adj Close'].pct_change()\n",
        "dataset = dataset.dropna()\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 3,
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Adj Close</th>\n",
              "      <th>Close</th>\n",
              "      <th>High</th>\n",
              "      <th>Low</th>\n",
              "      <th>Open</th>\n",
              "      <th>Volume</th>\n",
              "      <th>Open_Close</th>\n",
              "      <th>High_Low</th>\n",
              "      <th>Increase_Decrease</th>\n",
              "      <th>Buy_Sell_on_Open</th>\n",
              "      <th>Buy_Sell</th>\n",
              "      <th>Returns</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Date</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>2014-01-03</th>\n",
              "      <td>4.00</td>\n",
              "      <td>4.00</td>\n",
              "      <td>4.00</td>\n",
              "      <td>3.88</td>\n",
              "      <td>3.98</td>\n",
              "      <td>22887200</td>\n",
              "      <td>-0.005025</td>\n",
              "      <td>0.030928</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>0.012658</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2014-01-06</th>\n",
              "      <td>4.13</td>\n",
              "      <td>4.13</td>\n",
              "      <td>4.18</td>\n",
              "      <td>3.99</td>\n",
              "      <td>4.01</td>\n",
              "      <td>42398300</td>\n",
              "      <td>-0.029925</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>0.032500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2014-01-07</th>\n",
              "      <td>4.18</td>\n",
              "      <td>4.18</td>\n",
              "      <td>4.25</td>\n",
              "      <td>4.11</td>\n",
              "      <td>4.19</td>\n",
              "      <td>42932100</td>\n",
              "      <td>0.002387</td>\n",
              "      <td>0.034063</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0.012106</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2014-01-08</th>\n",
              "      <td>4.18</td>\n",
              "      <td>4.18</td>\n",
              "      <td>4.26</td>\n",
              "      <td>4.14</td>\n",
              "      <td>4.23</td>\n",
              "      <td>30678700</td>\n",
              "      <td>0.011820</td>\n",
              "      <td>0.028986</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2014-01-09</th>\n",
              "      <td>4.09</td>\n",
              "      <td>4.09</td>\n",
              "      <td>4.23</td>\n",
              "      <td>4.05</td>\n",
              "      <td>4.20</td>\n",
              "      <td>30667600</td>\n",
              "      <td>0.026190</td>\n",
              "      <td>0.044444</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>-0.021531</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "            Adj Close  Close  High   Low  Open    Volume  Open_Close  \\\n",
              "Date                                                                   \n",
              "2014-01-03       4.00   4.00  4.00  3.88  3.98  22887200   -0.005025   \n",
              "2014-01-06       4.13   4.13  4.18  3.99  4.01  42398300   -0.029925   \n",
              "2014-01-07       4.18   4.18  4.25  4.11  4.19  42932100    0.002387   \n",
              "2014-01-08       4.18   4.18  4.26  4.14  4.23  30678700    0.011820   \n",
              "2014-01-09       4.09   4.09  4.23  4.05  4.20  30667600    0.026190   \n",
              "\n",
              "            High_Low  Increase_Decrease  Buy_Sell_on_Open  Buy_Sell   Returns  \n",
              "Date                                                                           \n",
              "2014-01-03  0.030928                  1                 1         1  0.012658  \n",
              "2014-01-06  0.047619                  1                 1         1  0.032500  \n",
              "2014-01-07  0.034063                  0                 1         0  0.012106  \n",
              "2014-01-08  0.028986                  0                 0         0  0.000000  \n",
              "2014-01-09  0.044444                  0                 0         1 -0.021531  "
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 3,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:08.515Z",
          "iopub.execute_input": "2020-07-01T01:06:08.523Z",
          "shell.execute_reply": "2020-07-01T01:06:08.698Z",
          "iopub.status.idle": "2020-07-01T01:06:08.559Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = dataset.drop(['Adj Close', 'Buy_Sell'], axis=1)\n",
        "Y = dataset['Buy_Sell'].values"
      ],
      "outputs": [],
      "execution_count": 4,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:08.569Z",
          "iopub.execute_input": "2020-07-01T01:06:08.576Z",
          "iopub.status.idle": "2020-07-01T01:06:08.586Z",
          "shell.execute_reply": "2020-07-01T01:06:09.475Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print(X.shape)\n",
        "print(Y.shape)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(1257, 10)\n",
            "(1257,)\n"
          ]
        }
      ],
      "execution_count": 5,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:08.596Z",
          "iopub.execute_input": "2020-07-01T01:06:08.602Z",
          "iopub.status.idle": "2020-07-01T01:06:08.615Z",
          "shell.execute_reply": "2020-07-01T01:06:09.478Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = np.array(X).reshape(1257,-1)\n",
        "y = np.array(Y).reshape(1257,-1)"
      ],
      "outputs": [],
      "execution_count": 6,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:08.627Z",
          "iopub.execute_input": "2020-07-01T01:06:08.636Z",
          "iopub.status.idle": "2020-07-01T01:06:08.647Z",
          "shell.execute_reply": "2020-07-01T01:06:09.480Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.preprocessing import StandardScaler\n",
        "from sklearn import decomposition\n",
        "\n",
        "# Create a scaler object\n",
        "sc = StandardScaler()\n",
        "\n",
        "# Fit the scaler to the features and transform\n",
        "X_std = sc.fit_transform(X)"
      ],
      "outputs": [],
      "execution_count": 7,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:08.666Z",
          "iopub.execute_input": "2020-07-01T01:06:08.674Z",
          "iopub.status.idle": "2020-07-01T01:06:09.447Z",
          "shell.execute_reply": "2020-07-01T01:06:09.484Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a pca object with the 2 components as a parameter\n",
        "pca = decomposition.PCA(n_components=2)\n",
        "\n",
        "# Fit the PCA and transform the data\n",
        "X_std_pca = pca.fit_transform(X_std)"
      ],
      "outputs": [],
      "execution_count": 8,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:09.456Z",
          "iopub.execute_input": "2020-07-01T01:06:09.462Z",
          "iopub.status.idle": "2020-07-01T01:06:09.528Z",
          "shell.execute_reply": "2020-07-01T01:06:09.605Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# View the new feature data's shape\n",
        "X_std_pca.shape"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 9,
          "data": {
            "text/plain": [
              "(1257, 2)"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 9,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:09.540Z",
          "iopub.execute_input": "2020-07-01T01:06:09.547Z",
          "iopub.status.idle": "2020-07-01T01:06:09.561Z",
          "shell.execute_reply": "2020-07-01T01:06:09.609Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# View the new feature data\n",
        "X_std_pca"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 10,
          "data": {
            "text/plain": [
              "array([[-1.44899117,  0.76442088],\n",
              "       [-1.13188616,  1.61698256],\n",
              "       [-1.13629614,  0.62013725],\n",
              "       ...,\n",
              "       [ 3.40484299,  0.20020157],\n",
              "       [ 3.47729094,  1.02990627],\n",
              "       [ 3.20984775,  0.1234806 ]])"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 10,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-01T01:06:09.570Z",
          "iopub.execute_input": "2020-07-01T01:06:09.578Z",
          "iopub.status.idle": "2020-07-01T01:06:09.591Z",
          "shell.execute_reply": "2020-07-01T01:06:09.613Z"
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "codemirror_mode": {
        "version": 3,
        "name": "ipython"
      },
      "nbconvert_exporter": "python",
      "version": "3.5.5",
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "pygments_lexer": "ipython3"
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    },
    "nteract": {
      "version": "0.23.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}